package com.wlb365.financing.modules.trade.service.impl;

import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import com.wlb365.financing.common.constants.ApiConstants;
import com.wlb365.financing.common.exception.AdminException;
import com.wlb365.financing.modules.trade.request.TradeQueryDetailReq;
import com.wlb365.financing.modules.trade.request.TradeQueryPageReq;
import com.wlb365.financing.modules.trade.request.TradeQueryProcessDataDetailReq;
import com.wlb365.financing.modules.trade.request.TradeQueryProcessDataListReq;
import com.wlb365.financing.modules.trade.response.TradeProcessDataListResult;
import com.wlb365.financing.modules.trade.response.TradeQueryDetailRes;
import com.wlb365.financing.modules.trade.response.TradeQueryPageRes;
import com.wlb365.financing.common.utils.SdkHelper;
import com.wlb365.financing.modules.trade.response.TradeQueryProcessDataDetailRes;
import com.wlb365.financing.modules.trade.response.TradeQueryProcessDataRes;
import com.wlb365.financing.modules.trade.response.TradeQueryProcessRes;
import com.wlb365.financing.modules.trade.service.ITradeService;
import com.wlb365.financing.modules.trade.vo.TradeQueryVO;
import com.wlb365.sdk.client.SdkClient;
import com.wlb365.sdk.model.CommonRequest;
import com.wlb365.sdk.model.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 交易数据实现类.
 *
 * @author zwh
 * @version 1.0
 */
@Slf4j
@Service
public class TradeServiceImpl implements ITradeService {

    @Autowired
    private SdkClient sdkClient;

    @Override
    public TradeQueryPageRes queryTrade(final TradeQueryVO queryVO) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("current", queryVO.getCurrent());
        params.put("size", queryVO.getSize());
        params.put("contractNo", queryVO.getContractNo());
        params.put("supplierId", queryVO.getSupplierId());
        params.put("coreEnterpriseId", queryVO.getCoreEnterpriseId());
        return SdkHelper.execute(new TradeQueryPageReq(), params);
    }

    @Override
    public TradeQueryDetailRes queryTradeDetail(final String id) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("tradeId", id);
        return SdkHelper.execute(new TradeQueryDetailReq(), params);
    }

    @Override
    public TradeQueryProcessRes queryTradeProcess(String id) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.FS_QUERY_TRADE_PROCESS);
        Map<String, Object> params = Maps.newHashMap();
        params.put("tradeId", id);
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TradeQueryProcessRes tradeQueryProcessDataRes = JSONUtil.toBean(commonResponse.getData(), TradeQueryProcessRes.class);
        return tradeQueryProcessDataRes;
    }

    @Override
    public TradeQueryProcessDataRes queryTradeProcessData(String processId) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.FS_QUERY_TRADE_PROCESS_DATA);
        Map<String, Object> params = Maps.newHashMap();
        params.put("processId", processId);
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TradeQueryProcessDataRes tradeQueryProcessDataRes = JSONUtil.toBean(commonResponse.getData(), TradeQueryProcessDataRes.class);
        return tradeQueryProcessDataRes;
    }

    @Override
    public TradeQueryProcessDataDetailRes queryProcessDataDetail(TradeQueryProcessDataDetailReq tradeQueryProcessDataDetailReq) {
        CommonRequest commonRequest = new CommonRequest(ApiConstants.FS_QUERY_TRADE_PROCESS_DATA_DETAIL);
        Map<String, Object> params = Maps.newHashMap();
        params.put("processId", tradeQueryProcessDataDetailReq.getProcessId());
        params.put("stepNum", tradeQueryProcessDataDetailReq.getStepNum());
        commonRequest.setBizModel(params);
        CommonResponse commonResponse =  sdkClient.execute(commonRequest);
        TradeQueryProcessDataDetailRes tradeQueryProcessDataDetailRes = JSONUtil.toBean(commonResponse.getData(), TradeQueryProcessDataDetailRes.class);
        return tradeQueryProcessDataDetailRes;
    }

    @Override
    public TradeProcessDataListResult queryProcessDataDetailList(String tradeId, String templateId, String dataType) {

        Map<String, Object> params = Maps.newHashMap();
        params.put("tradeId", tradeId);
        params.put("processDataType", dataType);
        params.put("templateId", templateId);

        TradeProcessDataListResult result = SdkHelper.execute(new TradeQueryProcessDataListReq(), params);
        if (!ApiConstants.SUCCESS_CODE.equals(result.getCode())) {
            log.error("获取阶段数据异常：" + result.getMsg() + result.getSubMsg());
            throw new AdminException("获取阶段数据异常");
        }

        return result;
    }
}
